/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lenovo.framework.base.BaseDialogFragment;
import com.lenovo.framework.base.BaseListAdapter;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.MSConstants.FragmentType;
import com.lenovo.mso2o.ui.adapter.ConditionSelectAdapter;
import com.lenovo.mso2o.ui.adapter.MenuSelectAdapter;
import com.lenovo.mso2o.util.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by lit on 2015/11/26.
 */
public class ChoiceConditionDialogFragment extends BaseDialogFragment {
    public static final String STRING_LIST = "string_array_list";
    public static final java.lang.String IS_MENU_OR_NOT = "is_menu_or_not";
    public static final String POSITION = "position";
    private  int position;
    private ArrayList<String> datas;
    private boolean isMenu;
    private BaseListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position=getArguments().getInt(POSITION);
        datas = getArguments().getStringArrayList(STRING_LIST);
        isMenu = getArguments().getBoolean(IS_MENU_OR_NOT);
    }

    public void dismiss() {
        super.dismiss();
        if(mInterface != null)
            mInterface.onRetrieveDialogFragmentData(new Bundle(), FragmentType.DIALOG_CHOICE_FRAGMENT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.choice_condition_dialogfragment, container);
        ListView orderContentChoiceCondition = (ListView) view.findViewById(R.id.order_content_choice_condition);
        if(isMenu){
            adapter= new MenuSelectAdapter(datas, getActivity());
        }else {
            adapter = new ConditionSelectAdapter(datas);
        }
        orderContentChoiceCondition.setAdapter(adapter);
        adapter.setIndex(position);
        orderContentChoiceCondition.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                String itemString = datas.get(position);
                //{"text":"距离上门服务地址太远","index":0}
                //{"text":"拍照签到","index":1}
                try {
                    JSONObject itemJson = new JSONObject(itemString);
                    Bundle bundle = new Bundle();
                    bundle.putInt(POSITION, position);
                    bundle.putString("text", itemJson.getString("text"));
                    try {
                        bundle.putString("value",itemJson.getString("value"));
                    }catch (Exception e){}
                    if (getActivity().getString(R.string.gps_say_you_are_too_long).equals(itemJson.getString("text"))) {
                        return;
                    }
                    adapter.setIndex(position);
                    mInterface.onRetrieveDialogFragmentData(bundle, FragmentType.DIALOG_CHOICE_FRAGMENT);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dismiss();
            }
        });
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
//                        int downY = (int) event.getY() + outHeight * 2;
                        int downY = (int) event.getY();
                        if (isOutDistrict(downY, v)) {
                            dismiss();
                        }
                        break;
                }
                return false;
            }
        });
        return view;
    }
}
